home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / GXCACHE.C < prev    next >
C/C++ Source or Header  |  1992-03-06  |  11KB  |  349 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gxcache.c */
  21. /* Character cache routines for Ghostscript library */
  22. #include "gx.h"
  23. #include "memory_.h"
  24. #include "gserrors.h"
  25. #include "gxfixed.h"
  26. #include "gxmatrix.h"
  27. #include "gspaint.h"
  28. #include "gzstate.h"
  29. #include "gzdevice.h"            /* requires gsstate.h */
  30. #include "gzcolor.h"
  31. #include "gzpath.h"
  32. #include "gxcpath.h"
  33. #include "gxdevmem.h"
  34. #include "gxchar.h"
  35. #include "gxcache.h"
  36. #include "gxfont.h"
  37. #include "gxfdir.h"
  38.  
  39. extern ulong gs_next_ids(P1(uint));
  40.  
  41. /* Define the size of the cache structures. */
  42. /* We round the size of a cached_char so that */
  43. /* an immediately following bitmap will be properly aligned. */
  44. const uint cached_char_sizeof =
  45.   sizeof(cached_char) + (-sizeof(cached_char) & 3);
  46. #define cc_bits(cc) ((byte *)(cc) + cached_char_sizeof)
  47. const uint cached_fm_pair_sizeof = sizeof(cached_fm_pair);
  48.  
  49. /* Define the hash chain for a (code, fm_pair) key. */
  50. #define chars_head(dir, code, pair)\
  51.   &(dir)->chars[((uint)(code) + ((uint)(pair) << 4)) & (dir)->chars_mask]
  52.  
  53. /* Forward references */
  54. private void shorten_cached_char(P3(gs_font_dir *, cached_char *, uint));
  55. private void purge_fm_pair(P2(gs_font_dir *, cached_fm_pair *));
  56.  
  57. /* Initialize the character cache. */
  58. void
  59. gx_char_cache_init(register gs_font_dir *dir)
  60. {    cached_char_head *cdata = (cached_char_head *)dir->cdata;
  61.     int i;
  62.     cached_fm_pair *pair;
  63.     dir->bsize = 0;
  64.     dir->msize = 0;
  65.     dir->csize = 0;
  66.     dir->mnext = 0;
  67.     dir->cnext = 0;
  68.     cdata->pair = 0;
  69.     cdata->size = dir->cdata_size;
  70.     memset((char *)dir->chars, 0,
  71.            (dir->chars_mask + 1) * sizeof(cached_char *));
  72.     for ( i = dir->mmax, pair = dir->mdata; --i >= 0; pair++ )
  73.       fm_pair_set_free(pair);
  74. }
  75.  
  76. /* Allocate storage for caching a rendered character, */
  77. /* and set up the memory device. */
  78. /* Return the cached_char if OK, 0 if too big. */
  79. cached_char *
  80. gx_alloc_char_bits(gs_font_dir *dir, gx_device_memory *dev,
  81.   ushort iwidth, ushort iheight)
  82. {    ulong isize, cdsize;
  83.     cached_char_head *cch;
  84. #define hcc ((cached_char *)cch)
  85.     cached_char *cc;
  86.     uint fsize = 0;
  87.     byte *bits;
  88.     dev->width = iwidth;
  89.     dev->height = iheight;
  90.     isize = gdev_mem_bitmap_size(dev);    /* sets raster */
  91.     if ( dev->raster != 0 && iheight > dir->upper / dev->raster )
  92.         return 0;        /* too big */
  93.     cdsize = isize + cached_char_sizeof;
  94.     if ( cdsize >= dir->cmax )
  95.         return 0;        /* too big */
  96.     /* Look for and/or free enough space. */
  97.     cch = (cached_char_head *)(dir->cdata + dir->cnext);
  98.     cc = hcc;
  99.     for ( ; ; )
  100.       { if ( (byte *)cc + fsize == dir->cdata + dir->cdata_size )
  101.           cch = (cached_char_head *)dir->cdata, cc = hcc, fsize = 0;
  102.         if ( !cc_head_is_free(cch) )
  103.           { /* Free the character */
  104.         cached_char **pcc = chars_head(dir, hcc->code, cch->pair);
  105.         while ( *pcc != hcc )
  106.           pcc = &(*pcc)->next;
  107.         *pcc = hcc->next; /* remove from chain */
  108.         gx_free_cached_char(dir, hcc);
  109.           }
  110.         fsize += cch->size;
  111.         cc->head.size = fsize;
  112.         if ( fsize == cdsize ||
  113.          fsize >= cdsize + sizeof(cached_char_head)
  114.            )
  115.           break;        /* enough room here */
  116.         cch = (cached_char_head *)((byte *)cc + fsize);
  117.       }
  118. #undef hcc
  119.     if ( fsize > cdsize )
  120.       shorten_cached_char(dir, cc, fsize - cdsize);
  121. #ifdef DEBUG
  122. if ( gs_debug['k'] | gs_debug['K'] )
  123.     dprintf4("[k]adding 0x%lx:%u(%u,%u)\n",
  124.              (ulong)cc, (uint)cdsize, iwidth, iheight);
  125. #endif
  126.     bits = cc_bits(cc);
  127.     memset((char *)bits, 0, (uint)isize);
  128.     cc->width = iwidth;
  129.     cc->height = iheight;
  130.     cc->raster = dev->raster;
  131.     cc->head.pair = 0;    /* not linked in yet */
  132.     dev->base = bits;
  133.     (*dev->procs->open_device)((gx_device *)dev);    /* initialize */
  134.     dir->csize++;
  135.     dir->bsize += cdsize;
  136.     dir->cnext = (byte *)cc + cdsize - dir->cdata;
  137.     return cc;
  138. }
  139.  
  140. /* Remove a character from the cache. */
  141. void
  142. gx_free_cached_char(gs_font_dir *dir, cached_char *cc)
  143. {    dir->cnext = (byte *)cc - dir->cdata;
  144.     dir->csize--;
  145.     dir->bsize -= cc->head.size;
  146.     if ( cc->head.pair != 0 )
  147.        {    /* might be allocated but not added to table yet */
  148.         cc->head.pair->num_chars--;
  149.        }
  150.     cc_set_free(cc);
  151. }
  152.  
  153. /* Look up, and if necessary add, a font/matrix pair in the cache */
  154. cached_fm_pair *
  155. gx_lookup_fm_pair(register gs_state *pgs)
  156. {    float    mxx = pgs->char_tm.xx, mxy = pgs->char_tm.xy,
  157.         myx = pgs->char_tm.yx, myy = pgs->char_tm.yy;
  158.     gs_font *font = pgs->font;
  159.     register gs_font_dir *dir = font->dir;
  160.     register cached_fm_pair *pair = dir->mdata + dir->mnext;
  161.     int count = dir->mmax;
  162.     long uid = -1;
  163.     cached_fm_pair *mend;
  164.     if ( font->FontType != ft_composite )
  165.        {    uid = font->data.base.UniqueID;
  166.         if ( uid != -1 ) font = 0;
  167.        }
  168.     while ( count-- )
  169.        {    if ( pair == dir->mdata ) pair += dir->mmax;
  170.         pair--;
  171.         if (    pair->font == font && pair->UniqueID == uid &&
  172.             pair->mxx == mxx && pair->mxy == mxy &&
  173.             pair->myx == myx && pair->myy == myy
  174.            )
  175.           return pair;
  176.        }
  177.     /* Add the pair to the cache */
  178.     mend = dir->mdata + dir->mmax;
  179.     if ( dir->msize == dir->mmax ) /* cache is full */
  180.       { /* Prefer an entry with num_chars == 0, if any. */
  181.         for ( count = dir->mmax; --count >= 0 && pair->num_chars != 0; )
  182.           if ( ++pair == mend ) pair = dir->mdata;
  183.         purge_fm_pair(dir, pair);
  184.       }
  185.     else
  186.       { /* Look for an empty entry.  (We know there is one.) */
  187.         while ( !fm_pair_is_free(pair) )
  188.           if ( ++pair == mend ) pair = dir->mdata;
  189.       }
  190.     dir->msize++;
  191.     dir->mnext = pair + 1 - dir->mdata;
  192.     if ( dir->mnext == dir->mmax ) dir->mnext = 0;
  193.     pair->font = font;
  194.     pair->UniqueID = uid;
  195.     pair->mxx = mxx, pair->mxy = mxy;
  196.     pair->myx = myx, pair->myy = myy;
  197.     pair->num_chars = 0;
  198.     return pair;
  199. }
  200.  
  201. /* Add a character to the cache */
  202. void
  203. gx_add_cached_char(gs_font_dir *dir, gx_device_memory *dev,
  204.   cached_char *cc, cached_fm_pair *pair)
  205. {    cc->id = gs_next_ids(1);
  206.     /* Make sure the bits are in the right order */
  207.     /* to use as a source. */
  208.     gdev_mem_ensure_byte_order(dev);
  209.     /* Add the new character at the tail of its chain. */
  210.        {    register cached_char **head =
  211.           chars_head(dir, cc->code, pair);
  212.         while ( *head != 0 ) head = &(*head)->next;
  213.         *head = cc;
  214.         cc->next = 0;
  215.         cc->head.pair = pair;
  216.         pair->num_chars++;
  217.        }
  218.     /* Discard the memory device overhead that follows the bits. */
  219.      { uint diff = gdev_mem_bitmap_size(dev) - cc->raster * cc->height;
  220.        if ( diff >= sizeof(cached_char_head) )
  221.          { shorten_cached_char(dir, cc, diff);
  222.            dir->bsize -= diff;
  223. #ifdef DEBUG
  224. if ( gs_debug['K'] )
  225.            dprintf2("[K]shortening 0x%lx by %u\n", (ulong)cc,
  226.             diff);
  227. #endif
  228.          }
  229.      }
  230. }
  231.  
  232. /* Look up a character in the cache. */
  233. /* Return the cached_char or 0. */
  234. cached_char *
  235. gx_lookup_cached_char(gs_state *pgs, cached_fm_pair *pair, char_code ccode)
  236. {    gs_font_dir *dir = pgs->font->dir;
  237.     register cached_char *cc = *chars_head(dir, ccode, pair);
  238.     while ( cc != 0 )
  239.       { if ( cc->code == ccode && cc->head.pair == pair )
  240.           return cc;
  241.         cc = cc->next;
  242.       }
  243.     return 0;
  244. }
  245.  
  246. /* Copy a cached character to the screen. */
  247. /* Assume the caller has already done gx_color_load, */
  248. /* and the color is not a halftone. */
  249. /* Return 0 if OK, 1 if we couldn't do the operation but no error */
  250. /* occurred, or a negative error code. */
  251. int
  252. gx_image_cached_char(register gs_show_enum *penum, register cached_char *cc)
  253. {    register gs_state *pgs = penum->pgs;
  254.     int x, y, w, h;
  255.     int code;
  256.     gs_fixed_point pt;
  257.     gx_device *dev = pgs->device->info;
  258.     gx_device_clip cdev;
  259.     code = gx_path_current_point_inline(pgs->path, &pt);
  260.     if ( code < 0 ) return code;
  261.     /* Abort if the device color isn't pure. */
  262.     if ( !penum->color_loaded )
  263.        {    if ( !color_is_pure(pgs->dev_color) )
  264.             return 1;    /* can't use cache */
  265.         penum->color_loaded = 1;
  266.        }
  267.     /* If the character doesn't lie entirely within the */
  268.     /* quick-check clipping rectangle, we have to */
  269.     /* set up an intermediate clipping device. */
  270.     pt.x -= cc->offset.x;
  271.     x = fixed2int_var_rounded(pt.x) + penum->ftx;
  272.     pt.y -= cc->offset.y;
  273.     y = fixed2int_var_rounded(pt.y) + penum->fty;
  274.     w = cc->width;
  275.     h = cc->height;
  276. #ifdef DEBUG
  277. if ( gs_debug['K'] )
  278.     dprintf3("[K]copying 0x%lx, offset=(%g,%g)\n", (ulong)cc,
  279.          fixed2float(-cc->offset.x), fixed2float(-cc->offset.y)),
  280.     dprintf6("   at (%g,%g)+(%d,%d)->(%d,%d)\n", fixed2float(pt.x),
  281.          fixed2float(pt.y), penum->ftx, penum->fty, x, y);
  282. #endif
  283.     if (    x < penum->cxmin || x + w > penum->cxmax ||
  284.         y < penum->cymin || y + h > penum->cymax
  285.        )
  286.        {    cdev = gs_clip_device;
  287.         cdev.target = dev;
  288.         cdev.list = pgs->clip_path->list;
  289.         dev = (gx_device *)&cdev;
  290.         (*dev->procs->open_device)(dev);
  291. #ifdef DEBUG
  292. if ( gs_debug['K'] )
  293.         dputs("[K](clipping)\n");
  294. #endif
  295.        }
  296.     /* Copy the bits. */
  297.     code = (*dev->procs->copy_mono)
  298.         (dev, cc_bits(cc), 0, cc->raster, cc->id,
  299.          x, y, w, h,
  300.          gx_no_color_index, pgs->dev_color->color1);
  301.     return ( code < 0 ? code : 0 );
  302. }
  303.  
  304. /* Purge from the caches all references to a given font. */
  305. void
  306. gs_purge_font_from_char_caches(gs_font_dir *dir, gs_font *font)
  307. {    cached_fm_pair *pair = dir->mdata;
  308.     int count = dir->mmax;
  309.     while ( count-- )
  310.       { if ( pair->font == font ) purge_fm_pair(dir, pair);
  311.         pair++;
  312.       }
  313. }
  314.  
  315. /* ------ Internal routines ------ */
  316.  
  317. /* Shorten a cached character. */
  318. /* diff >= sizeof(cached_char_head). */
  319. private void
  320. shorten_cached_char(gs_font_dir *dir, cached_char *cc, uint diff)
  321. {    cached_char_head *next;
  322.     if ( (byte *)cc + cc->head.size == dir->cdata + dir->cnext )
  323.       dir->cnext -= diff;
  324.     cc->head.size -= diff;
  325.     next = (cached_char_head *)((byte *)cc + cc->head.size);
  326.     cc_head_set_free(next);
  327.     next->size = diff;
  328. }
  329.  
  330. /* Purge from the caches all references to a given font/matrix pair. */
  331. private void
  332. purge_fm_pair(gs_font_dir *dir, cached_fm_pair *pair)
  333. {    int chi;
  334.     for ( chi = dir->chars_mask; pair->num_chars != 0; )
  335.       { cached_char **pcc = dir->chars + chi--;
  336.         while ( *pcc != 0 )
  337.           { cached_char *cc = *pcc;
  338.         if ( cc->head.pair == pair )
  339.           { gx_free_cached_char(dir, cc);
  340.             *pcc = cc->next;
  341.           }
  342.         else
  343.           pcc = &cc->next;
  344.           }
  345.       }
  346.     fm_pair_set_free(pair);
  347.     dir->msize--;
  348. }
  349.